var KOC = require("koc.common.utils");
var StateData = require("../controllers/statedata.socket");
var DALFactory = require("./dal/dal-factory");
var CommonUtils = require("./common-utils");
var Interface_YunClean = require("./interface-yunclean");

exports.Init = function (io) {
    var SocketIO = {
        Room: io.of("/room")
    };
    //房间服务重置
    DALFactory.Business.User_RoomReset(function (retValue) {
        if (retValue.hasError) {
            console.log("Socket.IO启动失败[结束直播]:" + retValue.message + "(" + retValue.errorCode + ")");
            return;
        }
        //开启socket服务
        SocketIO.Room.use(StateData.Init(function (err, session, socket, next) {
            if (err) next(err);
            next();
        })).on("connection", function (socket) {
            /////////////////////////////////
            //退出事件
            /////////////////////////////////
            socket.on("disconnect", function () {
                DALFactory.Business.RedisRoom_Disconnect({
                    User_ID: socket.UserID,
                    RoomID: socket.RoomID,
                    SocketID: socket.id
                }, function (retValue) {
                    //广播房间信息
                    if (retValue.returnObject && retValue.returnObject.RoomInfo) {
                        socket.in(socket.RoomID).emit("LiveBroadcast", {
                            Type: "LiveEnd-Disconnect",
                            RoomInfo: retValue.returnObject.RoomInfo
                        });
                    }
                    if (retValue.hasError || !retValue.returnObject.Out) {
                        return;
                    }
                    //广播房间用户退出
                    socket.in(socket.RoomID).emit("OutBroadcast", {
                        RoomUserInfo: retValue.returnObject.RoomUserInfo,
                        CreateDate: retValue.returnObject.CreateDate
                    });
                });
            });
            /////////////////////////////////
            //进入房间
            /////////////////////////////////
            socket.on("Join", function (id, mid, appt, sign) {
                var _parm = {
                    Mobile: {
                        User_ID: -1,
                        Mobile_ID: KOC.StringUtils.ToIntPositive(mid),
                        APP_Type: KOC.StringUtils.ToIntPositive(appt),
                        Sign: sign
                    }
                };
                KOC.Async.series([
                    //读取用户ID(移动端)
                    function (callback) {
                        if (_parm.Mobile.Mobile_ID <= 0 && _parm.Mobile.APP_Type <= 0) {
                            callback();
                            return;
                        }
                        DALFactory.Business.User_MobileAuthVerify({
                            Mobile_ID: _parm.Mobile.Mobile_ID,
                            APP_Type: _parm.Mobile.APP_Type,
                            Sign: _parm.Mobile.Sign
                        }, function (retValue) {
                            if (retValue.hasError) {
                                callback(retValue);
                                return;
                            }
                            _parm.Mobile.User_ID = retValue.returnObject.User_ID;
                            callback();
                        });
                    },
                    //读取用户状态数据(移动端)
                    function (callback) {
                        if (_parm.Mobile.User_ID <= 0) {
                            callback();
                            return;
                        }
                        DALFactory.Business.User_StateData(_parm.Mobile.User_ID, function (retValue) {
                            if (retValue.hasError) {
                                callback(retValue);
                                return;
                            }
                            StateData.InitMobile(socket, retValue.returnObject);
                            callback();
                        });
                    },
                    //进入房间
                    function (callback) {
                        socket.RoomID = KOC.StringUtils.ToIntPositive(id);
                        DALFactory.Business.RedisRoom_Join({
                            User_ID: socket.UserID,
                            NickName: socket.StateData.NickName,
                            RoomID: id,
                            SocketID: socket.id
                        }, function (retValue) {
                            if (retValue.hasError) {
                                callback(retValue);
                                return;
                            }
                            _parm.Join = retValue.returnObject.Join;
                            _parm.RoomInfo = retValue.returnObject.RoomInfo;
                            _parm.RoomAnchorInfo = retValue.returnObject.RoomAnchorInfo;
                            _parm.RoomUserInfo = retValue.returnObject.RoomUserInfo;
                            _parm.UserPackage = retValue.returnObject.UserPackage;
                            _parm.MountsInfo = retValue.returnObject.MountsInfo;
                            _parm.Attention = retValue.returnObject.Attention;
                            _parm.GiftFloat = retValue.returnObject.GiftFloat;
                            _parm.GiftHeadline = retValue.returnObject.GiftHeadline;
                            _parm.GiftPond = retValue.returnObject.GiftPond;
                            _parm.CreateDate = retValue.returnObject.CreateDate;
                            callback();
                        });
                    }
                ], function (retValue) {
                    if (retValue) {
                        socket.emit("Join", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    //socket进入房间
                    socket.join(_parm.RoomInfo.RoomID.toString()).RoomID = _parm.RoomInfo.RoomID;
                    //广播房间用户进入
                    if (_parm.Join) {
                        socket.in(socket.RoomID.toString()).emit("JoinBroadcast", {
                            RoomUserInfo: _parm.RoomUserInfo,
                            MountsInfo: _parm.MountsInfo,
                            CreateDate: _parm.CreateDate
                        });
                    }
                    //通知房间用户进入
                    socket.emit("Join", {
                        flag: 0,
                        message: "",
                        Data: {
                            RoomInfo: _parm.RoomInfo,
                            RoomAnchorInfo: _parm.RoomAnchorInfo,
                            RoomUserInfo: _parm.RoomUserInfo,
                            UserPackage: _parm.UserPackage,
                            MountsInfo: _parm.MountsInfo,
                            Attention: _parm.Attention,
                            GiftFloat: _parm.GiftFloat,
                            GiftHeadline: _parm.GiftHeadline,
                            GiftPond: _parm.GiftPond,
                            CreateDate: _parm.CreateDate
                        }
                    });
                });
            });
            /////////////////////////////////
            //用户列表
            /////////////////////////////////
            socket.on("UserList", function () {
                DALFactory.RedisRoom.Room_UserList.Get(socket.RoomID, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("UserList", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    var _RoomUserList = [];
                    KOC.Async.eachSeries(retValue.returnObject, function (ThisValue, callback) {
                        DALFactory.Business.RedisRoom_RoomUserInfo({
                            RoomID: socket.RoomID,
                            User_ID: ThisValue
                        }, function (retValue) {
                            if (!retValue.hasError) {
                                _RoomUserList.push(retValue.returnObject);
                            }
                            callback();
                        });
                    }, function () {
                        //通知用户列表
                        socket.emit("UserList", {
                            flag: 0,
                            message: "",
                            Data: _RoomUserList
                        });
                    });
                });
            });
            /////////////////////////////////
            //管理员列表
            /////////////////////////////////
            socket.on("AdminList", function () {
                DALFactory.User.User_Anchor_Admin.List(socket.RoomID, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("AdminList", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    var _RoomAdminList = [];
                    KOC.Lodash.forEach(retValue.returnObject, function (ThisValue) {
                        //计算等级信息
                        var _LevelInfo = CommonUtils.User.Level.fnLevelInfo(KOC.StringUtils.ToIntPositive(ThisValue.EXP));
                        //计算VIP信息
                        ThisValue.VIP_ExpireDay = 0;
                        if (ThisValue.VIP && ThisValue.VIP_Expire && KOC.Moment().isAfter(KOC.Moment(ThisValue.VIP_Expire))) {
                            ThisValue.VIP = false;
                        }
                        if (ThisValue.VIP && ThisValue.VIP_Expire) {
                            ThisValue.VIP_ExpireDay = KOC.Moment(ThisValue.VIP_Expire).diff(KOC.Moment(), "days") < 0 ? 0 : KOC.Moment(ThisValue.VIP_Expire).diff(KOC.Moment(), "days");
                        }
                        _RoomAdminList.push({
                            User_ID: ThisValue.User_ID,
                            NickName: ThisValue.NickName,
                            EXP: _LevelInfo.EXP,
                            EXP_Next: _LevelInfo.EXP_Next,
                            EXP_Difference: _LevelInfo.EXP_Difference,
                            Level: _LevelInfo.Level,
                            Level_Name: _LevelInfo.Level_Name,
                            Level_Next: _LevelInfo.Level_Next,
                            Level_Next_Name: _LevelInfo.Level_Next_Name,
                            VIP: !!ThisValue.VIP,
                            VIP_Expire: ThisValue.VIP_Expire,
                            VIP_ExpireDay: ThisValue.VIP_ExpireDay
                        });
                    });
                    //通知用户列表
                    socket.emit("AdminList", {
                        flag: 0,
                        message: "",
                        Data: KOC.Lodash.sortBy(_RoomAdminList, function (ThisValue) {
                            return ((ThisValue.VIP ? 100 : 0) + ThisValue.Level) * -1;
                        })
                    });
                });
            });
            /////////////////////////////////
            //黑名单列表
            /////////////////////////////////
            socket.on("BlackList", function () {
                DALFactory.User.User_Anchor_Blacklist.List(socket.RoomID, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("BlackList", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    var _RoomBlackList = [];
                    KOC.Lodash.forEach(retValue.returnObject, function (ThisValue) {
                        //计算黑名单时间
                        if (ThisValue.Join && ThisValue.Join_Expire && KOC.Moment().isAfter(KOC.Moment(ThisValue.Join_Expire))) {
                            ThisValue.Join = false;
                        }
                        if (ThisValue.Shutup && ThisValue.Shutup_Expire && KOC.Moment().isAfter(KOC.Moment(ThisValue.Shutup_Expire))) {
                            ThisValue.Shutup = false;
                        }
                        if (!ThisValue.Join && !ThisValue.Shutup) {
                            return true;
                        }
                        //计算等级信息
                        var _LevelInfo = CommonUtils.User.Level.fnLevelInfo(KOC.StringUtils.ToIntPositive(ThisValue.EXP));
                        //计算VIP信息
                        ThisValue.VIP_ExpireDay = 0;
                        if (ThisValue.VIP && ThisValue.VIP_Expire && KOC.Moment().isAfter(KOC.Moment(ThisValue.VIP_Expire))) {
                            ThisValue.VIP = false;
                        }
                        if (ThisValue.VIP && ThisValue.VIP_Expire) {
                            ThisValue.VIP_ExpireDay = KOC.Moment(ThisValue.VIP_Expire).diff(KOC.Moment(), "days") < 0 ? 0 : KOC.Moment(ThisValue.VIP_Expire).diff(KOC.Moment(), "days");
                        }
                        _RoomBlackList.push({
                            User_ID: ThisValue.User_ID,
                            NickName: ThisValue.NickName,
                            EXP: _LevelInfo.EXP,
                            EXP_Next: _LevelInfo.EXP_Next,
                            EXP_Difference: _LevelInfo.EXP_Difference,
                            Level: _LevelInfo.Level,
                            Level_Name: _LevelInfo.Level_Name,
                            Level_Next: _LevelInfo.Level_Next,
                            Level_Next_Name: _LevelInfo.Level_Next_Name,
                            VIP: !!ThisValue.VIP,
                            VIP_Expire: ThisValue.VIP_Expire,
                            VIP_ExpireDay: ThisValue.VIP_ExpireDay,
                            Black_Join: ThisValue.Join,
                            Black_Join_Expire: ThisValue.Join_Expire,
                            Black_Shutup: ThisValue.Shutup,
                            Black_Shutup_Expire: ThisValue.Shutup_Expire
                        });
                    });
                    //通知用户列表
                    socket.emit("BlackList", {
                        flag: 0,
                        message: "",
                        Data: KOC.Lodash.sortBy(_RoomBlackList, function (ThisValue) {
                            return ((ThisValue.VIP ? 100 : 0) + ThisValue.Level) * -1;
                        })
                    });
                });
            });
            /////////////////////////////////
            //房间守护列表
            /////////////////////////////////
            socket.on("GuardianList", function () {
                DALFactory.User.User_Anchor_Guardian.List_Guardian(socket.RoomID, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("GuardianList", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    KOC.Lodash.forEach(retValue.returnObject, function (ThisValue) {
                        //计算等级信息
                        var _LevelInfo = CommonUtils.User.Level.fnLevelInfo(KOC.StringUtils.ToIntPositive(ThisValue.EXP));
                        ThisValue.Level = _LevelInfo.Level;
                        ThisValue.Level_Name = _LevelInfo.Level_Name;
                        ThisValue.Level_Next = _LevelInfo.Level_Next;
                        ThisValue.Level_Next_Name = _LevelInfo.Level_Next_Name;
                    });
                    //通知用户列表
                    socket.emit("GuardianList", {
                        flag: 0,
                        message: "",
                        Data: KOC.Lodash.sortBy(retValue.returnObject, function (ThisValue) {
                            return ThisValue.Guardian_Expire * -1;
                        })//按守护到期时间降序排列
                    });
                });
            });
            /////////////////////////////////
            //开始直播
            /////////////////////////////////
            socket.on("Live", function (title, mode, notice) {
                if (socket.UserID != socket.RoomID) {
                    socket.emit("Live", {
                        flag: 10,
                        message: "您不是此房间的艺人,不能开始直播."
                    });
                    return;
                }
                if (!KOC.Lodash.includes([0, 11, 12], mode)) {
                    notice = mode;
                    mode = 0;
                }
                DALFactory.Business.RedisRoom_Live({
                    User_ID: socket.UserID,
                    SocketID: socket.id,
                    IsMobile: socket.IsMobile,
                    Mode: mode,
                    RoomTitle: title,
                    RoomNotice: notice,
                    Create_IP: socket.handshake.address
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("Live", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    //通知直播开始
                    socket.emit("Live", {
                        flag: 0,
                        message: "",
                        Data: {
                            Live_PUSH: retValue.returnObject.Live_PUSH,
                            RoomInfo: retValue.returnObject.RoomInfo,
                            CreateDate: retValue.returnObject.CreateDate
                        }
                    });
                    //广播房间信息
                    socket.in(socket.RoomID).emit("LiveBroadcast", {
                        Type: "Live",
                        RoomInfo: retValue.returnObject.RoomInfo
                    });
                });
            });
            /////////////////////////////////
            //结束直播
            /////////////////////////////////
            socket.on("LiveEnd", function () {
                var _IsAdmin = socket.StateData && socket.StateData.IsAdmin;
                if (socket.RoomID != socket.UserID && !_IsAdmin) {
                    socket.emit("LiveEnd", {
                        flag: "",
                        message: "您没有权限结束对此房直播"
                    });
                    return;
                }
                DALFactory.Business.RedisRoom_LiveEnd({
                    RoomID: socket.RoomID,
                    SocketID: socket.id,
                    IsAdmin: _IsAdmin
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("LiveEnd", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    //广播房间信息
                    SocketIO.Room.in(socket.RoomID).emit("LiveBroadcast", {
                        Type: "LiveEnd",
                        RoomInfo: retValue.returnObject.RoomInfo
                    });
                });
            });
            /////////////////////////////////
            //购买守护
            /////////////////////////////////
            socket.on("Guardian", function (id) {
                DALFactory.Business.RedisRoom_Guardian({
                    User_ID: socket.UserID,
                    User_ID_Guardian: socket.RoomID,
                    Guardian_ID: id,
                    Create_IP: socket.handshake.address
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("Guardian", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    //通知守护购买
                    socket.emit("Guardian", {
                        flag: 0,
                        message: "",
                        Data: {
                            RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                            RoomUserInfo: retValue.returnObject.RoomUserInfo,
                            GuardianInfo: retValue.returnObject.GuardianInfo,
                            CreateDate: retValue.returnObject.CreateDate
                        }
                    });
                    //广播守护购买
                    socket.in(socket.RoomID).emit("GuardianBroadcast", {
                        RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                        RoomUserInfo: retValue.returnObject.RoomUserInfo,
                        GuardianInfo: retValue.returnObject.GuardianInfo,
                        CreateDate: retValue.returnObject.CreateDate
                    });
                    //广播跑道
                    if (CommonUtils.Commodity.Guardian.Runway && CommonUtils.Commodity.Guardian.Runway > 0 && retValue.returnObject.Order_Amt >= CommonUtils.Commodity.Guardian.Runway) {
                        //广播礼物跑道(全服)
                        SocketIO.Room.emit("RunwayBroadcast", {
                            Type: "Guardian",
                            Data: {
                                RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                                RoomUserInfo: retValue.returnObject.RoomUserInfo,
                                GuardianInfo: retValue.returnObject.GuardianInfo,
                                CreateDate: retValue.returnObject.CreateDate
                            }
                        });
                    }
                    //广播用户升级
                    if (retValue.returnObject.UserUpgrade) {
                        SocketIO.Room.in(socket.RoomID).emit("UserLevelBroadcast", {
                            RoomUserInfo: retValue.returnObject.RoomUserInfo,
                            CreateDate: retValue.returnObject.CreateDate
                        });
                    }
                });
            });
            /////////////////////////////////
            //购买礼物
            /////////////////////////////////
            socket.on("Gift", function (id, num, userid, purchasemode) {
                DALFactory.Business.RedisRoom_Gift({
                    PurchaseMode: purchasemode,
                    User_ID_Anchor: socket.RoomID,
                    User_ID: socket.UserID,
                    User_ID_Gift: userid || socket.RoomID,
                    Gift_ID: id,
                    Num: num,
                    Create_IP: socket.handshake.address
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("Gift", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    //通知礼物购买
                    socket.emit("Gift", {
                        flag: 0,
                        message: "",
                        Data: {
                            GiftNum: retValue.returnObject.GiftNum,
                            GiftCount: retValue.returnObject.GiftCount,
                            GiftStreamer: retValue.returnObject.GiftStreamer,
                            GiftFloat: retValue.returnObject.GiftFloat,
                            RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                            RoomUserInfo: retValue.returnObject.RoomUserInfo,
                            RoomUserInfo_Gift: retValue.returnObject.RoomUserInfo_Gift,
                            UserPackage: retValue.returnObject.UserPackage,
                            GiftInfo: retValue.returnObject.GiftInfo,
                            GiftPond: retValue.returnObject.GiftPond,
                            LuckyCoin: retValue.returnObject.LuckyCoin,
                            LuckyMultipleList: retValue.returnObject.LuckyMultipleList,
                            CreateDate: retValue.returnObject.CreateDate
                        }
                    });
                    //广播礼物购买
                    socket.in(socket.RoomID).emit("GiftBroadcast", {
                        GiftNum: retValue.returnObject.GiftNum,
                        GiftCount: retValue.returnObject.GiftCount,
                        GiftStreamer: retValue.returnObject.GiftStreamer,
                        GiftFloat: retValue.returnObject.GiftFloat,
                        RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                        RoomUserInfo: retValue.returnObject.RoomUserInfo,
                        RoomUserInfo_Gift: retValue.returnObject.RoomUserInfo_Gift,
                        GiftInfo: retValue.returnObject.GiftInfo,
                        GiftPond: retValue.returnObject.GiftPond,
                        LuckyCoin: retValue.returnObject.LuckyCoin,
                        LuckyMultipleList: retValue.returnObject.LuckyMultipleList,
                        CreateDate: retValue.returnObject.CreateDate
                    });
                    //判断跑道
                    if (retValue.returnObject.GiftInfo.IsRunway !== false && retValue.returnObject.Order_Amt >= CommonUtils.Commodity.Gift.Runway) {
                        //广播礼物跑道(全服)
                        SocketIO.Room.emit("RunwayBroadcast", {
                            Type: "Gift",
                            Data: {
                                RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                                RoomUserInfo: retValue.returnObject.RoomUserInfo,
                                RoomUserInfo_Gift: retValue.returnObject.RoomUserInfo_Gift,
                                GiftNum: retValue.returnObject.GiftNum,
                                GiftInfo: retValue.returnObject.GiftInfo,
                                CreateDate: retValue.returnObject.CreateDate
                            }
                        });
                    }
                    //判断礼物头条
                    if (retValue.returnObject.GiftHeadline) {
                        //广播头条(全服)
                        SocketIO.Room.emit("GiftHeadlineBroadcast", retValue.returnObject.GiftHeadline);
                    }
                    //广播用户升级
                    if (retValue.returnObject.UserUpgrade) {
                        SocketIO.Room.in(socket.RoomID).emit("UserLevelBroadcast", {
                            RoomUserInfo: retValue.returnObject.RoomUserInfo,
                            CreateDate: retValue.returnObject.CreateDate
                        });
                    }
                });
            });
            /////////////////////////////////
            //购买喇叭(飞屏)
            /////////////////////////////////
            socket.on("Horn", function (mode, msg, purchasemode) {
                DALFactory.Business.RedisRoom_Horn({
                    PurchaseMode: purchasemode,
                    User_ID: socket.UserID,
                    User_ID_Anchor: socket.RoomID,
                    Mode: mode,
                    Message: msg,
                    Create_IP: socket.handshake.address
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("Horn", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    //通知喇叭购买
                    socket.emit("Horn", {
                        flag: 0,
                        message: "",
                        Data: {
                            RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                            RoomUserInfo: retValue.returnObject.RoomUserInfo,
                            UserPackage: retValue.returnObject.UserPackage,
                            HornInfo: retValue.returnObject.HornInfo,
                            HornMessage: retValue.returnObject.HornMessage,
                            CreateDate: new Date()
                        }
                    });
                    //广播喇叭购买
                    switch (retValue.returnObject.HornInfo.Mode) {
                        case "ROOM":
                            socket.in(socket.RoomID).emit("HornBroadcast", {
                                RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                                RoomUserInfo: retValue.returnObject.RoomUserInfo,
                                HornInfo: retValue.returnObject.HornInfo,
                                HornMessage: retValue.returnObject.HornMessage,
                                CreateDate: new Date()
                            });
                            break;
                        case "SERVER":
                            SocketIO.Room.emit("HornBroadcast", {
                                RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                                RoomUserInfo: retValue.returnObject.RoomUserInfo,
                                HornInfo: retValue.returnObject.HornInfo,
                                HornMessage: retValue.returnObject.HornMessage,
                                CreateDate: new Date()
                            });
                            break;
                    }
                    //广播用户升级
                    if (retValue.returnObject.UserUpgrade) {
                        SocketIO.Room.in(socket.RoomID).emit("UserLevelBroadcast", {
                            RoomUserInfo: retValue.returnObject.RoomUserInfo,
                            CreateDate: retValue.returnObject.CreateDate
                        });
                    }
                });
            });
            /////////////////////////////////
            //星星获取
            /////////////////////////////////
            socket.on("PomeloGet", function () {
                var Value = {};
                KOC.Async.series([
                    //领取星星
                    function (callback) {
                        DALFactory.Business.RedisRoom_PomeloGet({
                            RoomID: socket.RoomID,
                            User_ID: socket.UserID
                        }, function (retValue) {
                            if (retValue.hasError) {
                                callback(retValue);
                                return;
                            }
                            Value.RoomUserInfo = retValue.returnObject;
                            callback();
                        });
                    },
                    //读取房间用户Socket列表
                    function (callback) {
                        DALFactory.RedisRoom.Room_UserInfo_Socket.Get(Value.RoomUserInfo.RoomID, Value.RoomUserInfo.User_ID, function (retValue) {
                            if (retValue.hasError) {
                                callback(retValue);
                                return;
                            }
                            Value.RoomUserSocket = retValue.returnObject;
                            callback();
                        });
                    }
                ], function (retValue) {
                    if (retValue) {
                        socket.emit("PomeloGet", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    //通知用户星星领取
                    KOC.Lodash.forEach(Value.RoomUserSocket, function (ThisValue) {
                        if (ThisValue == socket.id) {
                            socket.emit("PomeloGet", {
                                flag: 0,
                                message: "",
                                Data: Value.RoomUserInfo
                            });
                            return true;
                        }
                        socket.to(ThisValue).emit("PomeloGet", {
                            flag: 0,
                            message: "",
                            Data: Value.RoomUserInfo
                        });
                    });
                });
            });
            /////////////////////////////////
            //星星赠送
            /////////////////////////////////
            socket.on("PomeloPresent", function (num) {
                DALFactory.Business.RedisRoom_PomeloPresent({
                    RoomID: socket.RoomID,
                    User_ID: socket.UserID,
                    Num: num,
                    Create_IP: socket.handshake.address
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("PomeloPresent", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    //通知星星赠送
                    socket.emit("PomeloPresent", {
                        flag: 0,
                        message: "",
                        Data: {
                            Num: retValue.returnObject.Num,
                            EXP: retValue.returnObject.EXP,
                            RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                            RoomUserInfo: retValue.returnObject.RoomUserInfo
                        }
                    });
                    //广播星星赠送
                    socket.in(socket.RoomID).emit("PomeloPresentBroadcast", {
                        Num: retValue.returnObject.Num,
                        EXP: retValue.returnObject.EXP,
                        RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                        RoomUserInfo: retValue.returnObject.RoomUserInfo,
                        CreateDate: retValue.returnObject.CreateDate
                    });
                    //广播用户升级
                    if (retValue.returnObject.UserUpgrade) {
                        SocketIO.Room.in(socket.RoomID).emit("UserLevelBroadcast", {
                            RoomUserInfo: retValue.returnObject.RoomUserInfo,
                            CreateDate: retValue.returnObject.CreateDate
                        });
                    }
                });
            });
            /////////////////////////////////
            //发送消息
            /////////////////////////////////
            socket.on("Message", function (msg, id, pte) {
                var Value = {};
                KOC.Async.series([
                    //过滤消息内容
                    function (callback) {
                        Interface_YunClean.AuditText({
                            Anchor_ID: socket.RoomID,
                            User_ID: socket.UserID,
                            Message_Content: msg,
                            Create_IP: socket.handshake.address
                        }, function (retValue) {
                            if (retValue.hasError) {
                                retValue.message = "非法的消息内容，已被系统阻止";
                                callback(retValue);
                                return;
                            }
                            callback();
                        });
                    },
                    //读取房间用户信息
                    function (callback) {
                        DALFactory.Business.RedisRoom_RoomUserInfo({
                            RoomID: socket.RoomID,
                            User_ID: socket.UserID
                        }, function (retValue) {
                            if (retValue.hasError) {
                                callback(retValue);
                                return;
                            }
                            if (retValue.returnObject.Guest) {
                                retValue.hasError = true;
                                retValue.errorCode = -1;
                                retValue.message = "游客能不发言,请先登录";
                                callback(retValue);
                                return;
                            }
                            // if (!retValue.returnObject.IsPhone) {
                            //     retValue.hasError = true;
                            //     retValue.errorCode = 11;
                            //     retValue.message = "绑定手机号码才能发言";
                            //     callback(retValue);
                            //     return;
                            // }
                            if (retValue.returnObject.RoomBlack) {
                                retValue.hasError = true;
                                retValue.errorCode = 10;
                                retValue.message = "您已经被管理员禁言至:" + (retValue.returnObject.RoomBlack_Shutup_Expire ? KOC.Moment(retValue.returnObject.RoomBlack_Shutup_Expire).format("YYYY/MM/DD HH:mm:ss") : "永久");
                                callback(retValue);
                                return;
                            }
                            if (pte && retValue.returnObject.Level < 3 && !(retValue.returnObject.RoomAnchor || retValue.returnObject.IsAdmin)) {
                                retValue.hasError = true;
                                retValue.errorCode = 1;
                                retValue.message = "用户等级3级以上才能进行私聊";
                                callback(retValue);
                                return;
                            }
                            Value.RoomUserInfo = retValue.returnObject;
                            Value.RoomUserInfo.IsMobile = socket.IsMobile ? socket.IsMobile : false;
                            callback();
                        });
                    },
                    //取得房间用户信息(接收人)
                    function (callback) {
                        if (!id) {
                            callback();
                            return;
                        }
                        DALFactory.Business.RedisRoom_RoomUserInfo({
                            RoomID: socket.RoomID,
                            User_ID: id
                        }, function (retValue) {
                            if (retValue.hasError) {
                                callback(retValue);
                                return;
                            }
                            Value.RoomUserInfo_Receiver = retValue.returnObject;
                            callback();
                        });
                    },
                    //读取房间用户Socket列表
                    function (callback) {
                        if (!pte || !Value.RoomUserInfo_Receiver) {
                            callback();
                            return;
                        }
                        DALFactory.RedisRoom.Room_UserInfo_Socket.Get(socket.RoomID, id, function (retValue) {
                            if (retValue.hasError) {
                                callback(retValue);
                                return;
                            }
                            Value.RoomUserSocket_Receiver = retValue.returnObject;
                            callback();
                        });
                    },
                    //写入艺人热度
                    function (callback) {
                        DALFactory.Business.User_AnchorHot({
                            Type: "Message",
                            Value: CommonUtils.Anchor.Hot.Message,
                            User_ID: socket.RoomID
                        }, function () {
                            callback();
                        });
                    }
                ], function (retValue) {
                    if (retValue) {
                        socket.emit("Message", {
                            flag: retValue.errorCode,
                            message: retValue.message,
                            Data: msg
                        });
                        return;
                    }
                    //通知消息发送人
                    socket.emit("Message", {
                        flag: 0,
                        message: "",
                        Data: {
                            Private: pte ? true : false,
                            Message: msg,
                            RoomUserInfo: Value.RoomUserInfo,
                            RoomUserInfo_Receiver: Value.RoomUserInfo_Receiver,
                            CreateDate: new Date()
                        }
                    });
                    //通知消息接收人(私聊)
                    if (pte && Value.RoomUserInfo_Receiver) {
                        KOC.Lodash.forEach(Value.RoomUserSocket_Receiver, function (ThisValue) {
                            if (ThisValue == socket.id) {
                                socket.emit("MessageBroadcast", {
                                    Private: true,
                                    Message: msg,
                                    RoomUserInfo: Value.RoomUserInfo,
                                    RoomUserInfo_Receiver: Value.RoomUserInfo_Receiver
                                });
                                return true;
                            }
                            socket.to(ThisValue).emit("MessageBroadcast", {
                                Private: true,
                                Message: msg,
                                RoomUserInfo: Value.RoomUserInfo,
                                RoomUserInfo_Receiver: Value.RoomUserInfo_Receiver,
                                CreateDate: new Date()
                            });
                        });
                        return;
                    }
                    //广播房间所有人消息
                    socket.in(socket.RoomID).emit("MessageBroadcast", {
                        Private: false,
                        Message: msg,
                        RoomUserInfo: Value.RoomUserInfo,
                        RoomUserInfo_Receiver: Value.RoomUserInfo_Receiver,
                        CreateDate: new Date()
                    });
                });
            });
            /////////////////////////////////
            //房间管理设置
            //id:被设置用户ID
            //admin:设置类型 true:设为房管 false:取消房管
            /////////////////////////////////
            socket.on("AdminSet", function (id, admin) {
                if (socket.UserID != socket.RoomID) {
                    socket.emit("AdminSet", {
                        flag: 1,
                        message: "您不是本房间的艺人,不能进行此操作"
                    });
                    return;
                }
                DALFactory.Business.RedisRoom_AdminSet({
                    RoomID: socket.RoomID,
                    User_ID: id,
                    RoomAdmin: admin
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("AdminSet", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    //通知管理员设置
                    socket.emit("AdminSet", {
                        flag: 0,
                        message: "",
                        Data: {
                            RoomUserInfo: retValue.returnObject.RoomUserInfo,
                            CreateDate: retValue.returnObject.CreateDate
                        }
                    });
                    //被操作用户没有在房间直接返回
                    if (!retValue.returnObject.RoomUserInfo) {
                        return;
                    }
                    //广播管理员设置
                    socket.in(socket.RoomID).emit("AdminSetBroadcast", {
                        RoomUserInfo: retValue.returnObject.RoomUserInfo,
                        CreateDate: retValue.returnObject.CreateDate
                    });
                });
            });
            /////////////////////////////////
            //黑名单设置
            //type:操作类型 1：禁言，2：踢人，3：取消禁言，4：取消踢人
            //mode:禁言或踢人,的类型，["NEVER", "YEAR", "MONTH", "WEEK", "DAY", "HOUR", "MINUTE"]
            //value:操作类型 禁言或踢人的时间值
            /////////////////////////////////
            socket.on("BlackSet", function (id, type, mode, value) {
                DALFactory.Business.RedisRoom_BlackSet({
                    RoomID: socket.RoomID,
                    User_ID: id,
                    User_ID_Admin: socket.UserID,
                    Type: type,
                    Mode: mode,
                    Value: value
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("BlackSet", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    //通知黑名单设置
                    socket.emit("BlackSet", {
                        flag: 0,
                        message: "",
                        Data: {
                            Type: retValue.returnObject.Type,
                            RoomUserInfo_Admin: retValue.returnObject.RoomUserInfo_Admin,
                            RoomUserInfo: retValue.returnObject.RoomUserInfo,
                            RoomUserBlackInfo: retValue.returnObject.RoomUserBlackInfo,
                            CreateDate: retValue.returnObject.CreateDate
                        }
                    });
                    //广播房间信息
                    if (retValue.returnObject.RoomInfo) {
                        socket.in(socket.RoomID).emit("LiveBroadcast", {
                            Type: "LiveEnd-BlackSet",
                            RoomInfo: retValue.returnObject.RoomInfo
                        });
                    }
                    //被操作用户没有在房间直接返回
                    if (!retValue.returnObject.RoomUserInfo) {
                        return;
                    }
                    //广播黑名单设置
                    socket.in(socket.RoomID).emit("BlackSetBroadcast", {
                        Type: retValue.returnObject.Type,
                        RoomUserInfo_Admin: retValue.returnObject.RoomUserInfo_Admin,
                        RoomUserInfo: retValue.returnObject.RoomUserInfo,
                        RoomUserBlackInfo: retValue.returnObject.RoomUserBlackInfo,
                        CreateDate: retValue.returnObject.CreateDate
                    });
                    //踢出用户
                    if (retValue.returnObject.RoomUserSocket) {
                        KOC.Lodash.each(retValue.returnObject.RoomUserSocket, function (ThisValue) {
                            var _ThisSocket = SocketIO.Room.sockets[ThisValue];
                            if (_ThisSocket) {
                                _ThisSocket.disconnect().RoomID = null;
                            }
                        });
                    }
                });
            });
            /////////////////////////////////
            //房间冻结
            //block:true:冻结 false:解冻
            /////////////////////////////////
            socket.on("BlockSet", function (block, data) {
                //判断权限
                if (!socket.StateData || !socket.StateData.IsAdmin) {
                    socket.emit("BlockSet", {
                        flag: "",
                        message: "您没有权限对此房间进行冻结"
                    });
                    return;
                }
                var Operation = "前台" + (block ? "冻结" : "解冻") + "房间";
                DALFactory.Business.RedisRoom_BlockSet({
                    RoomID: socket.RoomID,
                    Block: block,
                    Operation: Operation,
                    AdminID: socket.UserID,
                    Data: data,//操作原因
                    Create_IP: socket.handshake.address
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("BlockSet", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    //通知房间冻结
                    socket.emit("BlockSet", {
                        flag: 0,
                        message: "",
                        Data: !!block
                    });
                    if (retValue.returnObject) {
                        //广播房间信息
                        SocketIO.Room.in(socket.RoomID).emit("LiveBroadcast", {
                            Type: "LiveEnd-BlockSet",
                            RoomInfo: retValue.returnObject.RoomInfo
                        });
                    }
                });
            });
            /////////////////////////////////
            //抽奖
            /////////////////////////////////
            socket.on("Lottery", function (data) {
                DALFactory.Business.RedisRoom_Lottery({
                    User_ID: socket.UserID,//用户ID
                    User_ID_Anchor: socket.RoomID,//直播间ID
                    Lottery_ID: data.Lottery_ID,//抽奖ID
                    Create_IP: socket.handshake.address
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("Lottery", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    var parm = {
                        RoomUserInfo: retValue.returnObject.RoomUserInfo,
                        RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                        UserPackage: retValue.returnObject.UserPackage,
                        LotteryInfo: retValue.returnObject.LotteryInfo,
                        LotteryWinningList: retValue.returnObject.LotteryWinningList
                    };
                    //广播处理
                    var WinningList_Room = KOC.Lodash.filter(parm.LotteryWinningList, {Inform: "ROOM"});
                    var WinningList_Server = KOC.Lodash.filter(parm.LotteryWinningList, {Inform: "SERVER"});
                    socket.emit("Lottery", {
                        flag: 0,
                        message: "",
                        RoomUserInfo: parm.RoomUserInfo,
                        UserPackage: parm.UserPackage,
                        LotteryInfo: parm.LotteryInfo,
                        LotteryWinningList: parm.LotteryWinningList,
                        CreateDate: new Date()
                    });
                    //房间广播
                    if (WinningList_Room.length > 0) {
                        socket.in(socket.RoomID).emit("LotteryBroadcast", {
                            flag: 0,
                            message: "",
                            RoomUserInfo: parm.RoomUserInfo,
                            RoomAnchorInfo: parm.RoomAnchorInfo,
                            LotteryWinningList: WinningList_Room,
                            CreateDate: new Date()
                        });
                    }
                    //全服广播
                    if (WinningList_Server.length > 0) {
                        SocketIO.Room.emit("LotteryBroadcast", {
                            flag: 0,
                            message: "",
                            RoomUserInfo: parm.RoomUserInfo,
                            RoomAnchorInfo: parm.RoomAnchorInfo,
                            LotteryWinningList: WinningList_Server,
                            CreateDate: new Date()
                        });
                    }
                });
            });
            /////////////////////////////////
            //砸蛋
            /////////////////////////////////
            socket.on("PoundEgg", function (data) {
                DALFactory.Business.RedisRoom_PoundEgg({
                    User_ID: socket.UserID,//用户ID
                    User_ID_Anchor: socket.RoomID,//直播间ID
                    PoundEgg_ID: data.PoundEgg_ID,//ID
                    Create_IP: socket.handshake.address
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("PoundEgg", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    var parm = {
                        RoomUserInfo: retValue.returnObject.RoomUserInfo,
                        RoomAnchorInfo: retValue.returnObject.RoomAnchorInfo,
                        UserPackage: retValue.returnObject.UserPackage,
                        PoundEggInfo: retValue.returnObject.PoundEggInfo,
                        PoundEggWinningList: retValue.returnObject.PoundEggWinningList
                    };
                    //广播处理
                    var WinningList_Room = KOC.Lodash.filter(parm.PoundEggWinningList, {Inform: "ROOM"});
                    var WinningList_Server = KOC.Lodash.filter(parm.PoundEggWinningList, {Inform: "SERVER"});
                    socket.emit("PoundEgg", {
                        flag: 0,
                        message: "",
                        RoomUserInfo: parm.RoomUserInfo,
                        UserPackage: parm.UserPackage,
                        PoundEggInfo: parm.PoundEggInfo,
                        PoundEggWinningList: parm.PoundEggWinningList,
                        CreateDate: new Date()
                    });
                    //房间广播
                    if (WinningList_Room.length > 0) {
                        SocketIO.Room.in(socket.RoomID).emit("PoundEggBroadcast", {
                            flag: 0,
                            message: "",
                            RoomUserInfo: parm.RoomUserInfo,
                            RoomAnchorInfo: parm.RoomAnchorInfo,
                            PoundEggWinningList: WinningList_Room,
                            CreateDate: new Date()
                        });
                    }
                    //全服广播
                    if (WinningList_Server.length > 0) {
                        SocketIO.Room.emit("PoundEggBroadcast", {
                            flag: 0,
                            message: "",
                            RoomUserInfo: parm.RoomUserInfo,
                            RoomAnchorInfo: parm.RoomAnchorInfo,
                            PoundEggWinningList: WinningList_Server,
                            CreateDate: new Date()
                        });
                    }
                });
            });
            /////////////////////////////////
            //房间警告
            //type : 警告类型
            //content : 警告内容
            //Inform : 形式，房间通知，全服通知//通知类型:-1-默认不通知,1-通知艺人自己,2-通知房间内所有用户
            //integral : 扣分
            //operation : 操作，结束直播，冻结房间//操作类型:-1-警告,1-结束直播,2-冻结房间
            /////////////////////////////////
            socket.on("WarningSet", function (type, content, Inform, integral, operation) {
                if (!socket.StateData || !socket.StateData.IsLogin || !socket.StateData.IsAdmin) {
                    socket.emit("WarningSet", {
                        flag: "",
                        message: "您没有权限操作此房间"
                    });
                    return;
                }
                var Value = {
                    Integral: integral,
                    Type: KOC.StringUtils.ToString(type),
                    Operation: operation,
                    Inform: Inform,
                    Remark: KOC.StringUtils.ToString(content)
                };
                DALFactory.Business.RedisRoom_WarningSet({
                    User_ID_Admin: socket.UserID,
                    User_NickName_Admin: socket.StateData.NickName,
                    User_RealName_Admin: socket.StateData.RealName,
                    User_ID: socket.RoomID,
                    Integral: Value.Integral,
                    Type: Value.Type,
                    Operation: Value.Operation,
                    Inform: Value.Inform,
                    Remark: Value.Remark,
                    Create_IP: socket.handshake.address
                }, function (retValue) {
                    if (retValue.hasError) {
                        socket.emit("WarningSet", {
                            flag: retValue.errorCode,
                            message: retValue.message
                        });
                        return;
                    }
                    Value.Integral = retValue.returnObject.Integral;
                    Value.Operation = retValue.returnObject.Operation;
                    Value.Inform = retValue.returnObject.Inform;
                    Value.RoomAnchorSocket = retValue.returnObject.RoomAnchorSocket;
                    //通知操作人
                    socket.emit("WarningSetBroadcast", {
                        CreateDate: new Date(),
                        WarningInfo: {
                            Integral: Value.Integral,
                            Type: Value.Type,
                            Remark: Value.Remark,
                            Operation: Value.Operation
                        }
                    });
                    //通知艺人
                    if (Value.Inform == 1 && Value.RoomAnchorSocket && Value.RoomAnchorSocket.length > 0) {
                        KOC.Lodash.forEach(Value.RoomAnchorSocket, function (ThisValue) {
                            socket.to(ThisValue).emit("WarningSetBroadcast", {
                                CreateDate: new Date(),
                                WarningInfo: {
                                    Integral: Value.Integral,
                                    Type: Value.Type,
                                    Remark: Value.Remark,
                                    Operation: Value.Operation
                                }
                            });
                        });
                    }
                    //通知房间
                    if (Value.Inform == 2) {
                        socket.in(socket.RoomID).emit("WarningSetBroadcast", {
                            CreateDate: new Date(),
                            WarningInfo: {
                                Integral: Value.Integral,
                                Type: Value.Type,
                                Remark: Value.Remark,
                                Operation: Value.Operation
                            }
                        });
                    }
                    //结束直播
                    if (Value.Operation > 0) {
                        SocketIO.Room.in(socket.RoomID).emit("LiveBroadcast", {
                            Type: "LiveEnd-WarningSet-" + (Value.Operation == 1 ? "End" : "Block"),
                            RoomInfo: retValue.returnObject.RoomInfo
                        });
                    }
                });
            });
        });
    });
};